<template>
  <div class="app-container">
    <el-form size="small" :inline="true" :model="searchForm" class="demo-form-inline">
      <el-form-item label="关键词">
        <el-input clearable v-model="searchForm.keyword" placeholder="搜索"></el-input>
      </el-form-item>
      <el-form-item label="状态">
        <selected-status clearable @change="setStatus"
                         :options="[{label: '全部',value:''},{label:'启用',value:1},{label:'禁用',value:-1}]"
        ></selected-status>
      </el-form-item>
      <el-form-item label="优惠券类型">
        <selected-status clearable @change="setType"
                         :options="[{label: '全部',value:''},{label:'满减券',value:10},{label:'折扣券',value:20}]"
        ></selected-status>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="search">查询</el-button>
        <el-button title="添加" type="success" @click="$router.push({name:'couponForm'})">添加</el-button>
      </el-form-item>
    </el-form>

    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      border
      fit
      highlight-current-row
    >
      <el-table-column align="center" label="#" width="95" prop="coupon_id"/>

      <el-table-column label="类型" width="130">
        <template v-slot:default="{scope,row}">
          <el-tag :type="row.coupon_type==10?'success':'primary'">{{ row.coupon_type_desc }}</el-tag>
        </template>
      </el-table-column>

      <el-table-column label="名称" width="150" prop="name"/>
      <el-table-column label="是否启用" width="100" align="center">
        <template v-slot:default="{scope,row}">
          <el-switch v-model="row.status" :active-value="1" :inactive-value="-1" @change="changeStatus(row)"
          ></el-switch>
        </template>
      </el-table-column>
      <el-table-column label="排序" width="100" align="center" prop="sort"/>
      <el-table-column label="满减金额" width="100" align="center" prop="reduce_price"/>
      <el-table-column label="折扣" width="100" align="center" prop="discount"/>
      <el-table-column label="发放数量" width="100" align="center" prop="total_num"/>
      <el-table-column label="已领数量" width="100" align="center" prop="get_num"/>
      <el-table-column label="有效期类型" width="130">
        <template v-slot:default="{scope,row}">
          <el-tag :type="row.expire_type==10?'success':'primary'">{{ row.expire_type_desc }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="开始时间" width="180" align="center" prop="startTime"/>
      <el-table-column label="结束时间" width="180" align="center" prop="endTime"/>

      <el-table-column label="最后修改" width="180" align="center" prop="updated_at"/>
      <el-table-column align="center" prop="created_at" label="操作" width="150" fixed="right">
        <template v-slot:default="{scope,row}">
          <el-row>
            <el-button type="primary" icon="el-icon-edit" circle
                       @click="$router.push({name:'couponForm',query:{id:row.coupon_id}})"
            ></el-button>
            <el-button type="danger" icon="el-icon-delete" circle @click="del(row.coupon_id)"></el-button>
          </el-row>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit"
                @pagination="fetchData"
    />
  </div>

</template>

<script>
import api from '@/api/coupon'
import Pagination from '@/components/Pagination'
import selectedStatus from '@/components/tools/selectedStatus.vue'

export default {
  components: {
    Pagination, selectedStatus
  },
  filters: {},
  data() {
    return {
      list: null,
      listLoading: true,
      total: 0,
      listQuery: {
        page: 1,
        limit: 10,
        sort: ''
      },
      searchForm: {
        keyword: ''
      }
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    setStatus(value) {
      this.searchForm.status = value
      this.search()
    },
    setType(newValue) {
      this.searchForm.coupon_type = newValue
      this.search()
    },
    fetchData() {
      this.listLoading = true
      api.getList(this.listQuery).then(response => {

        this.list = response.data.list
        this.total = response.data.total
        this.listLoading = false
      })
    },
    search() {
      this.listQuery.page = 1
      this.listQuery = Object.assign(this.listQuery, this.searchForm)
      this.fetchData()
    },
    del(id) {
      this.$confirm('此操作将永久删除, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        api.del({ id: id }).then((res) => {
          this.fetchData()
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
        })

      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    changeStatus(row) {
      api.status({ id: row.coupon_id })
        .then(res => {
          this.$message({
            message: res.msg,
            type: 'success'
          })
        })
    }
  }

}
</script>
